Skip to main content

Overview

This module provides the BitVec structure for efficient storage and manipulation of binary vectors. BitVec is used extensively in LPN-based encryption and noise generation.

Structure

BitVec

Compact representation of a binary vector using packed 64-bit words.
size_t
Number of bits in the vector
std::vector<uint64_t>
Storage array where each uint64_t holds 64 bits
Bits are packed into 64-bit words for efficient storage and operations. A vector of n bits uses ceil(n/64) words.

Construction

make

Creates a new BitVec initialized to all zeros.
size_t
Number of bits in the vector
BitVec
New BitVec with n bits, all initialized to 0
Example:

Operations

xor_with

Performs in-place XOR with another BitVec.
const BitVec&
Vector to XOR with
Example:
If the vectors have different sizes, only the overlapping portion (minimum size) is affected.

popcnt

Counts the number of 1-bits in the vector (Hamming weight).
size_t
Number of bits set to 1
Example:
Uses the compiler’s __builtin_popcountll intrinsic for efficient population count on 64-bit words.

Utility functions

parity64

Computes the parity (XOR of all bits) of a 64-bit word in constant time.
uint64_t
Input word
int
0 if an even number of bits are set, 1 if odd
Example:
This function uses constant-time XOR shifts to compute parity, making it suitable for cryptographic applications where timing side-channels must be avoided.

Implementation details

Bit packing

Bits are stored in little-endian order within each 64-bit word:
  • Bit 0 is the LSB of w[0]
  • Bit 63 is the MSB of w[0]
  • Bit 64 is the LSB of w[1]
  • And so on…
Example:

Memory layout

For a BitVec with nbits bits:
  • Number of words: (nbits + 63) / 64
  • Memory usage: 8 * ((nbits + 63) / 64) bytes (plus overhead)
Example:

Usage patterns

LPN secret vector

Combining vectors

Computing inner product

Performance considerations

Optimization tips:
  • BitVec operations work on 64-bit words, providing 64x speedup over bit-by-bit operations
  • XOR operations are memory-bandwidth limited; keep vectors aligned
  • Population count (popcnt) uses hardware instructions on modern CPUs
  • Parity computation is constant-time for security

Constant-time operations

The parity64 function is implemented in constant time to prevent timing side-channels:
This approach ensures the execution time is independent of the input value.